﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Text.RegularExpressions;

namespace Web.Page
{
    public partial class XSHTGL_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改";
                this.tbRQ0.Attributes.Add("onclick", "show()");
                this.tbRQ1.Attributes.Add("onclick", "show()");
                initValue();
            }
        }

        public static string bh { get; set; }
        public static string khmc { get; set; }
        public static string khlxr { get; set; }
        public static string xsbm { get; set; }
        public static string xsryid { get; set; }
        public static string dhsj { get; set; }
        public static string chsj { get; set; }
        public static string chfs { get; set; }
        public static string htzt { get; set; }

        private void initValue()
        {
            string id = Request.QueryString["id"];
            XSHTGL_Dao dao = new XSHTGL_Dao();  
            XSHT xsh = dao.Query(id);
            bh = xsh.BH;
            khmc = xsh.KHMC;
            khlxr = xsh.KHLXR;  
            //xsbm = xsh.XSBMID.ToString();
            //xsryid = xsh.XSRYID.ToString();
            dhsj = xsh.DHSJ.ToString();
            chsj = xsh.CHSJ.ToString();
            chfs = xsh.CHFS;
            htzt = xsh.HTZT.ToString();

            // 销售部门ID     
            // 部门
            Departments_Dao daoDep1 = new Departments_Dao();
            List<Department> lsDep = daoDep1.QueryAll();
            this.ddlxsbmid.DataSource = lsDep;
            this.ddlxsbmid.DataTextField = "Name";
            this.ddlxsbmid.DataValueField = "ID";
            this.ddlxsbmid.DataBind();
            this.ddlxsbmid.Items.Insert(0, new ListItem("请选择", "0")); 

            // 销售合同编号
            this.tbBH.Text = bh;
            //List<XSHT> xsbmi = dao.QueryAll();
            //this.ddlbh.DataSource = xsbmi;
            //this.ddlbh.DataTextField = "BH";
            //this.ddlbh.DataValueField = "ID";
            //this.ddlbh.DataBind();
            //this.ddlbh.SelectedValue = id;
            //this.ddlbh.Items.Insert(0, new ListItem("请选择", "0")); 
            //合同名称
            this.tbMC.Text = khmc;
            //List<XSHT> ktmc = dao.QueryAll();
            //this.ddlktmc.DataSource = ktmc;
            //this.ddlktmc.DataTextField = "KHMC";
            //this.ddlktmc.DataValueField = "ID";
            //this.ddlktmc.DataBind();
            //this.ddlktmc.SelectedValue = id;
            //this.ddlktmc.Items.Insert(0, new ListItem("请选择", "0")); 
            //合同联系人
            this.tbktlxr.Text = khlxr;
            //List<XSHT> lxr = dao.QueryAll();
            //this.ddlhtlxr.DataSource = lxr;
            //this.ddlhtlxr.DataTextField = "KHLXR";
            //this.ddlhtlxr.DataValueField = "ID";
            //this.ddlhtlxr.DataBind();
            //this.ddlhtlxr.SelectedValue = id;
            //this.ddlhtlxr.Items.Insert(0, new ListItem("请选择", "0")); 
 

            //出货方式
            this.tbchfs.Text = chfs ;
            //List<XSHT> lschfs = dao.QueryAll();
            //this.ddlchfs.DataSource = lschfs;
            //this.ddlchfs.DataTextField = "CHFS";
            //this.ddlchfs.DataValueField = "ID";
            //this.ddlchfs.DataBind();
            //this.ddlchfs.SelectedValue = id;
            //this.ddlchfs.Items.Insert(0, new ListItem("请选择", "0")); 
            //合同状态
            this.ddlhtzt.SelectedIndex = int.Parse(htzt);
            //List<XSHT> lschzt = dao.QueryAll();
            //this.ddlhtzt.DataSource = lschzt;
            //this.ddlhtzt.DataTextField = "HTZT";
            //this.ddlhtzt.DataValueField = "ID";
            //this.ddlhtzt.DataBind();
            //this.ddlhtzt.SelectedValue = id;
            //this.ddlhtzt.Items.Insert(0, new ListItem("请选择", "0")); 
            //到货时间
            XSHT xsht = dao.Query(id);
            this.hdId.Value = xsht.ID.ToString();
            this.tbRQ0.Text = xsht.DHSJ.ToString();

            //出货时间
            this.tbRQ1.Text = xsht.CHSJ.ToString();
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            tbBH.Text = bh;
            tbMC.Text = khmc;
            tbktlxr.Text = khlxr;
            ddlxsbmid.Text = xsbm;
            ddlxsyid.Text = xsryid;
            tbRQ0.Text = dhsj;
            tbRQ1.Text = chsj;
            ddlhtzt.Text = htzt;
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {

            this.lbResult.Text = "";
            if (this.ddlxsbmid.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请输入或选择销售部门";
                return;
            }
            if (this.ddlxsyid.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请输入或选择销售人员";
                return;
            }
            if (this.tbBH.Text.Trim()=="")
            {
                this.lbResult.Text = "请输入或选择合同编号";
                return;
            }
            if (this.tbMC.Text.Trim() == "")
            {
                this.lbResult.Text = "请输入或选择合同名称";
                return;
            }
            if (this.tbktlxr.Text.Trim()=="")
            {
                this.lbResult.Text = "请输入或选择合同联系人";
                return;
            }

            if (this.tbRQ0.Text.Trim().Equals("0"))
            {
                this.lbResult.Text = "请输入或选择到货时间";
                return;
            }
            if (this.tbRQ1.Text.Trim().Equals("0"))
            {
                this.lbResult.Text = "请输入或选择出货时间";
                return;
            }
            if (this.tbchfs.Text.Trim().Equals("0"))
            {
                this.lbResult.Text = "请输入或选择出货方式";
                return;
            }
            if (this.ddlhtzt.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请输入或选择合同状态";
                return;
            }
            string id = Request.QueryString["id"];
            XSHT entity = new XSHT()
            {
                ID = Guid.Parse(id),
                BH = this.tbBH.Text.Trim(),
                KHMC = this.tbMC.Text,
                KHLXR = this.tbktlxr.Text,
                XSBMID = Guid.Parse(this.ddlxsbmid.SelectedValue),
                XSRYID = Guid.Parse(this.ddlxsyid.SelectedValue),
                DHSJ = DateTime.Parse(this.tbRQ0.Text.Trim()),
                CHSJ = DateTime.Parse(this.tbRQ1.Text.Trim()),
                CHFS = this.tbchfs.Text.Trim(),
                HTZT = int.Parse(this.ddlhtzt.Text)
            };

            string resultMessage = "";
            try
            {
                XSHTGL_Dao dao = new XSHTGL_Dao();
                int result = dao.Update(entity);
                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/XSHTGL_Query.aspx?op=" + this.hdOp.Value);
        }

        protected class UserNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
        protected void ddlxsbmid_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlxsyid.Items.Clear();
            string depId = ddlxsbmid.SelectedValue;
            if (depId != "0")
            {
                Guid guid = Guid.Parse(depId);
                using (var context = new Dao.ErpEntities())
                {
                    var query = from d in context.UserInDepartmentSet
                                join u in context.UserSet
                                on d.UserId equals u.Id
                                where d.DepartmentId == guid
                                select new
                                {
                                    Id = d.UserId,
                                    Name = u.Name
                                };
                    List<UserNames> list = new List<UserNames>();
                    if (query.Count() > 0)
                    {
                        foreach (var q in query)
                        {
                            UserNames un = new UserNames();
                            un.Id = q.Id.ToString();
                            un.Name = q.Name;
                            list.Add(un);
                        }
                    }

                    this.ddlxsyid.DataSource = list;
                    this.ddlxsyid.DataTextField = "Name";
                    this.ddlxsyid.DataValueField = "Id";
                    this.ddlxsyid.DataBind();
                }
            }

            this.ddlxsyid.Items.Insert(0, new ListItem("请选择", "0"));
        }
    }
}